package com.foot.travel.activity;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.foot.travel.R;
import com.foot.travel.event.NetRefreshEvent;
import com.foot.travel.viewholder.MyGalleryItemViewHolder;
import com.koalac.ironman.common.adapter.CommonAdapter;
import com.koalac.ironman.common.module.ModuleActivity;
import com.koalac.ironman.net.pojo.request.DisplayMyGalleryList;
import com.koalac.ironman.net.pojo.response.GalleryItem;
import com.koalac.ironman.viewhelper.ViewInject;
import com.koalac.ironman.widget.PullToRefreshView;
import com.koalac.ironman.widget.PullToRefreshView.OnFooterRefreshListener;
import com.koalac.ironman.widget.PullToRefreshView.OnHeaderRefreshListener;
import com.squareup.otto.Subscribe;

public class MyGalleryListActivity extends ModuleActivity implements
	OnHeaderRefreshListener, OnFooterRefreshListener {

    @ViewInject(id = R.id.beautiful_photo_container)
    PullToRefreshView mBeautifulPhotoContainer;
    @ViewInject(id = R.id.beautiful_photo_listview)
    ListView mBeautifulPhotoListview;
    @ViewInject(id = R.id.no_data_layout)
    View mNoDataLayout;

    public CommonAdapter<GalleryItem> mGalleryItemAdapter;
    private DisplayMyGalleryList mDisplayMyGalleryList;
    public List<GalleryItem> mGalleryItemList = new ArrayList<GalleryItem>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setNavigationTitle("我的旅图");
	setBackButtonVisibility(true);
	mBeautifulPhotoContainer
		.setOnHeaderRefreshListener(MyGalleryListActivity.this);
	mBeautifulPhotoContainer
		.setOnFooterRefreshListener(MyGalleryListActivity.this);
	getGalleryList();
    }

    private void getGalleryList() {
	mDisplayMyGalleryList = new DisplayMyGalleryList();
	mGalleryItemAdapter = new CommonAdapter<GalleryItem>(
		MyGalleryItemViewHolder.class, mGalleryItemList,
		MyGalleryListActivity.this, R.layout.my_gallery_item);
	mBeautifulPhotoListview.setAdapter(mGalleryItemAdapter);
	loadData(GalleryItem.class, LoadState.init, mDisplayMyGalleryList,
		mBeautifulPhotoContainer, mGalleryItemAdapter, mGalleryItemList);
    }

    @Override
    protected void showNoDataFoundToast() {
	mNoDataLayout.setVisibility(View.VISIBLE);
	((TextView)mNoDataLayout.findViewById(R.id.text)).setText("还未发过最美旅图");
    }

    @Override
    protected void initLayout(ViewGroup rootContentView) {
	addContentView(R.layout.activity_most_beautiful_photo);
    }
    
    @Subscribe
    public void onRefresh(NetRefreshEvent netRefreshEvent) {
	setNetworkVisibility(false);
	loadData(GalleryItem.class, LoadState.init, mDisplayMyGalleryList,
		mBeautifulPhotoContainer, mGalleryItemAdapter, mGalleryItemList);
    }

    @Override
    public void onFooterRefresh(PullToRefreshView view) {
	loadData(GalleryItem.class, LoadState.more, mDisplayMyGalleryList,
		mBeautifulPhotoContainer, mGalleryItemAdapter, mGalleryItemList);
    }

    @Override
    public void onHeaderRefresh(PullToRefreshView view) {
	loadData(GalleryItem.class, LoadState.refresh, mDisplayMyGalleryList,
		mBeautifulPhotoContainer, mGalleryItemAdapter, mGalleryItemList);
    }

}
